<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公众号" prop="appId">
        <el-select v-model="queryParams.appId">
          <el-option v-for="item in appIds" :label="item.name" :value="item.appId" />
        </el-select>
      </el-form-item>

      <el-form-item label="出发地址" prop="startAddress">
        <el-input
          v-model="queryParams.startAddress"
          placeholder="请输入出发地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="目的地" prop="endAddress">
        <el-input
          v-model="queryParams.endAddress"
          placeholder="请输入目的地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="携带物品" prop="appendices">
        <el-select
          v-model="queryParams.appendices"
          placeholder="请选择携带物品"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mp:shunfengche-order:create']"
          >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mp:shunfengche-order:export']"
          >导出
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->

    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" />

      <el-table-column label="出发地址" align="center" prop="startAddress" />

      <el-table-column label="目的地" align="center" prop="endAddress" />

      <el-table-column label="出发时间" align="center" prop="startTime" />

      <el-table-column label="乘车人数" align="center" prop="personNum" />
      <el-table-column label="联系电话" align="center" prop="personPhone" />

      <el-table-column label="携带物品" align="center" prop="appendices">
        <template v-slot="scope">
          <dict-tag
            :type="DICT_TYPE.INFRA_BOOLEAN_STRING"
            :value="scope.row.appendices"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          {{ orderStatus(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="订单来源" align="center" prop="source">
        <template v-slot="scope">
          {{ scope.row.source == 2 ? "后台" : "App" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mp:shunfengche-order:update']"
          >
            修改
          </el-button>
          <el-button
            v-show="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCancel(scope.row)"
            v-hasPermi="['mp:shunfengche-order:update']"
          >
            取消
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReSend(scope.row)"
            v-hasPermi="['mp:shunfengche-order:update']"
          >
            重派
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mp:shunfengche-order:delete']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      v-dialogDrag
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公众号" prop="appId">
          <el-select v-model="form.appId" placeholder="请选择公众号">
            <el-option v-for="item in appIds" :label="item.name" :value="item.appId" />
          </el-select>
        </el-form-item>
        <el-form-item label="出发地址" prop="startAddress">
          <el-input v-model="form.startAddress" placeholder="请输入出发地址" />
        </el-form-item>
        <el-form-item label="目的地" prop="endAddress">
          <el-input v-model="form.endAddress" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="出发时间" prop="startTimeClock">
          <el-select
            v-model="form.startTimeType"
            placeholder="请选择"
            style="width: 30%"
            @change="startTimeChange"
          >
            <el-option
              v-for="item in startTimeTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-time-select
            v-model="form.startTimeClock"
            v-show="showTimeSelect"
            :picker-options="startTimeOptions"
            placeholder="选择时间"
          >
          </el-time-select>
        </el-form-item>
        <el-form-item label="乘车人数" prop="personNum">
          <el-input
            :max="10"
            :maxlength="2"
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.personNum"
            placeholder="请输入乘车人数"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="personPhone">
          <el-input
            :maxlength="11"
            :minlength="11"
            clearable
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.personPhone"
            placeholder="请输入联系电话"
          />
        </el-form-item>
        <el-form-item label="携带物品" prop="appendices">
          <el-select v-model="form.appendices" placeholder="请选择携带物品">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createShunfengcheOrder,
  updateShunfengcheOrder,
  deleteShunfengcheOrder,
  getShunfengcheOrder,
  cancelShunfengcheOrder,
  reSendShunfengcheOrder,
  getShunfengcheOrderPage,
  exportShunfengcheOrderExcel,
} from "@/api/mp/shunfengcheOrder";
import { getSimpleAccounts } from "@/api/mp/account";

export default {
  name: "ShunfengcheOrder",
  components: {},
  data() {
    let checkPhone = (rule, value, callback) => {
      var reg = /^(13[0-9]|14[0-9]|15[0-9]|16[6]|18[0-9]|19[6,9]|17[0-9])\d{8}$/i;
      if (!reg.test(value)) {
        callback(new Error("请输入正确的手机号码格式"));
      } else {
        callback();
      }
    };
    let checkStartTime = (rule, value, callback) => {
      if (!this.form.startTimeClock || !this.form.startTimeType) {
        callback(new Error("出发时间不能为空"));
      } else {
        callback();
      }
    };
    return {
      appIds: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公众号顺风车订单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        appId: null,
        startAddress: null,
        endAddress: null,
        appendices: null,
        createTime: [],
      },
      // 表单参数
      form: {
        startTime: "",
      },
      // 表单校验
      rules: {
        appId: [{ required: true, message: "公众号不能为空", trigger: "change" }],
        startAddress: [{ required: true, message: "出发地址不能为空", trigger: "blur" }],
        endAddress: [{ required: true, message: "目的地不能为空", trigger: "blur" }],
        startTimeClock: [
          {
            required: true,
            validator: checkStartTime,
            trigger: "blur",
          },
        ],
        personNum: [{ required: true, message: "乘车人数不能为空", trigger: "blur" }],
        personPhone: [{ validator: checkPhone, required: true, trigger: "blur" }],
        appendices: [{ required: true, message: "携带物品不能为空", trigger: "change" }],
      },
      showTimeSelect: false,
    };
  },
  created() {
    getSimpleAccounts().then((response) => {
      this.appIds = response.data;
      // 默认选中第一个
      if (this.appIds.length > 0) {
        this.queryParams.appId = this.appIds[0].appId;
        this.form.appId = this.appIds[0].appId;
      }
      this.getList();
    });
  },
  methods: {
    startTimeClockChange(value) {
      var startTimeTypeOptions = this.startTimeTypeOptions;
      var selected = startTimeTypeOptions.find(
        (item) => (item.value = this.form.startTimeType)
      );
      var selectDefault = "现在";
      if (selected) {
        this.form.startTime = selected.label + value;
      } else {
        this.form.startTime = selectDefault;
      }
    },
    startTimeChange(value) {
      this.showTimeSelect = true;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getShunfengcheOrderPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        appId: undefined,
        startAddress: undefined,
        endAddress: undefined,
        startTime: undefined,
        personNum: undefined,
        appendices: undefined,
        startTimeClock: undefined,
        startTimeType: "1",
        personPhone: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公众号顺风车订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getShunfengcheOrder(id).then((response) => {
        this.form = response.data;
        let startTimeTypeLabel = this.form.startTime.substr(0, 2);
        var selected = this.startTimeTypeOptions.find(
          (item) => item.label == startTimeTypeLabel
        );
        this.open = true;
        this.title = "修改公众号顺风车订单";
        this.showTimeSelect = true;
      });
    },
    handleReSend(row) {
      const id = row.id;
      reSendShunfengcheOrder(id).then((response) => {
        if (response.data == true) {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        } else {
          this.$modal.msgError("操作失败");
        }
      });
    },
    handleCancel(row) {
      const id = row.id;
      cancelShunfengcheOrder(id).then((response) => {
        if (response.data == true) {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        } else {
          this.$modal.msgError("操作失败");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        var selected = this.startTimeTypeOptions.find(
          (item) => item.value == this.form.startTimeType
        );
        this.form.startTime = selected.label + this.form.startTimeClock;

        // 修改的提交
        if (this.form.id != null) {
          updateShunfengcheOrder(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
            this.reset();
          });
          return;
        }
        // 添加的提交
        createShunfengcheOrder(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
          this.reset();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除公众号顺风车订单编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteShunfengcheOrder(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgError("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有公众号顺风车订单数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportShunfengcheOrderExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "公众号顺风车订单.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    orderStatus(status) {
      if (status == 0) {
        return "待接单";
      } else if (status == 1) {
        return "已接单";
      } else if (status == 2) {
        return "超时已取消";
      } else if (status == 3) {
        return "顾客取消";
      } else if (status == 4) {
        return "完成";
      } else if (status == 5) {
        return "后台取消";
      }
      return "未知";
    },
  },
  computed: {
    startTimeTypeOptions() {
      var today = new Date();
      var nowHour = today.getHours();
      var nowMinutes = today.getMinutes();
      var result = [
        {
          value: "1",
          label: "现在",
        },
        {
          value: "2",
          label: "今天",
        },
        {
          value: "3",
          label: "明天",
        },
        {
          value: "4",
          label: "后天",
        },
      ];
      var minHour = 8;
      var maxHour = 18;
      if (nowHour < minHour) {
        result.splice(0, 2);
      } else if (nowHour > maxHour) {
        result.splice(0, 2);
      }
      return result;
    },
    startTimeOptions() {
      var today = new Date();
      var nowHour = today.getHours();
      var nowMinutes = today.getMinutes();
      var startHour = nowHour > 10 ? nowHour : "0" + nowHour;
      var startMinute = nowMinutes > 10 ? nowMinutes : "0" + nowMinutes;
      var start = startHour + ":" + nowMinutes;
      if (this.form.startTimeType == 1) {
        return {
          start: start,
          step: "00:30",
          end: start,
        };
      } else {
        return {
          start: "00:00",
          step: "00:30",
          end: "23:00",
        };
      }
    },
  },
};
</script>
